﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IronLua.Runtime
{
    internal sealed class CallFrameCollection
        : IReadOnlyList<CallFrame>
    {

        private readonly Thread thread;
        private readonly List<CallFrame> frames;

        public CallFrameCollection(Thread thread)
        {
            this.thread = thread;
            this.frames = new List<CallFrame>(64) { CallFrame.Host };
        }

        public CallFrame    this[int index] { get { return this.frames[index]; } }
        public int          Count { get { return this.frames.Count; } }
        public CallFrame    Current { get { return this.frames[this.Count - 1]; } set { this.frames[this.Count - 1] = value; } }

        public void Push(CallFrame frame)
        {
            this.frames.Add(frame);
        }

        public bool TryProp(out CallFrame frame)
        {
            if (this.Count < 1)
            {
                frame = default(CallFrame);
                return false;
            }

            var removeIndex = this.Count - 1;
            frame = this.frames[removeIndex];
            this.frames.RemoveAt(removeIndex);
            return true;
        }

        public IEnumerator<CallFrame> GetEnumerator()
        {
            return this.frames.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}
